package com.dwd.hrm.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.dwd.hrm.client.AuthClient;
import com.dwd.hrm.domain.*;
import com.dwd.hrm.exception.GlobalException;
import com.dwd.hrm.mapper.VipBaseMapper;
import com.dwd.hrm.mapper.VipUserMapper;
import com.dwd.hrm.service.IVipUserService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.dwd.hrm.util.AjaxResult;
import com.dwd.hrm.util.StrUtils;
import com.dwd.hrm.util.encrypt.MD5;
import com.dwd.hrm.utils.ValidUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 会员登录账号 服务实现类
 * </p>
 *
 * @author wendi
 * @since 2021-03-11
 */
@Service
public class VipUserServiceImpl extends ServiceImpl<VipUserMapper, VipUser> implements IVipUserService {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private VipBaseMapper vipBaseMapper;
    @Autowired
    private VipUserMapper vipUserMapper;
    @Autowired
    private PasswordEncoder passwordEncoder;
    @Autowired
    private AuthClient authClient;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void register(RegisterDto registerDto) {
        //校验数据
        String imageCode = registerDto.getImageCode();
        String mobile = registerDto.getMobile();
        String password = registerDto.getPassword();
        Integer regChannel = registerDto.getRegChannel();
        String smsCode = registerDto.getSmsCode();
        //校验数据
        checkData(imageCode, mobile, password, regChannel, smsCode);
        //存入user表
        Date date = new Date();
        VipUser vipUser = new VipUser();
        vipUser.setCreateTime(date.getTime());
        vipUser.setPhone(mobile);
        vipUser.setUpdateTime(date.getTime());
        //盐值
//        vipUser.setSalt(UUID.randomUUID().toString());
//        vipUser.setPassword(MD5.getMD5(vipUser.getSalt() + password));
        vipUser.setPassword(passwordEncoder.encode(password));
        //绑定手机位状态
        vipUser.addBitState(VipUser.BIND_PHONE);
        //先插入loginuser表
        AjaxResult ajaxResult = authClient.save(vipUser);
        if (!ajaxResult.isSuccess()) {
            throw new RuntimeException("添加loginuser表失败");
        }
        String string = ajaxResult.getResultObj().toString();
        //转JSON字符串传输.map转不了对象
        LoginUser loginUser = JSON.parseObject(string, LoginUser.class);

        vipUser.setLoginId(loginUser.getId());

        baseMapper.insert(vipUser);
        //绑定基本信息
        VipBase vipBase = new VipBase();
        //随机生成四位邀请码
        String invitation = StrUtils.getRandomString(4);
        //设置邀请码
        vipBase.setInvitation(invitation);
        vipBase.setRegChannel(regChannel);
        vipBase.setCreateTime(date.getTime());
        //邀请码
        //处理逻辑再 个人中心生成自己的链接 "http://127.0.0.1:6003/reg.phone.html?code=" +邀请码
        if (StringUtils.isNotBlank(registerDto.getInvitation())) {
            VipUser vipUser2 = vipBaseMapper.queryByInvitation(registerDto.getInvitation());
            ValidUtils.assertNotNull(vipUser2, "邀请码错误");
            vipBase.setReferId(vipUser2.getId());
        }
        vipBase.setRegTime(date.getTime());
        //登陆用户
        vipBase.setSsoId(vipUser.getId());
        vipBaseMapper.insert(vipBase);
    }

    private void checkData(String imageCode, String mobile, String password, Integer regChannel, String smsCode) {
        ValidUtils.assertNotNull(mobile, "手机不能为空");

        ValidUtils.assertNotNull(password, "密码不能为空");
        ValidUtils.assertNotNull(imageCode, "图片验证码不能为空");
        ValidUtils.assertNotNull(regChannel, "系统异常");
        ValidUtils.assertNotNull(smsCode, "验证码不能为空");
        //校验短信验证码
        SendSmsVo sendSmsVo = (SendSmsVo) redisTemplate.opsForValue().get("REG" + mobile);
        ValidUtils.assertNotNull(sendSmsVo, "验证码过期");
        ValidUtils.assertNotEqual(sendSmsVo.getCode(), smsCode, "验证码不一致");
    }
}
